/*
 * VHLeagueHistoryTab.java
 * Created on 16 juil. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.tabs;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.ui.Widget;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.VHMainController;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.ui.base.VHTabItem;
import com.hockeo.vhweb.client.ui.widget.VHSeasonSelector;
import com.hockeo.vhweb.client.ui.widget.VHStandingsGrid;
import com.hockeo.vhweb.client.ui.widget.VHStandingsTypeSelector;
import com.hockeo.vhweb.client.utils.VHJsData;
import com.hockeo.vhweb.client.utils.enums.VHJsStandingsType;

/**
 * Displays a season selector and a standings grid displaying the final standings of the
 * given season.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHLeagueHistoryTab extends VHTabItem
{
  private VHJsData                d_league;
  private VHSeasonSelector        d_seasonSelector;
  private VHStandingsTypeSelector d_standingsTypeSelector;
  private BorderLayoutData        d_layoutDataCenter;

  // center widget
  //
  private Widget                  d_widgetCurrent;
  private VHStandingsGrid         d_standingsGrid;
  private ContentPanel            d_panelNoResult;

  public VHLeagueHistoryTab( VHController controller, VHJsData league )
  {
    super( controller, VHMain.getConstants().history() );

    d_league = league;

    setIcon( VHMain.ICON_HISTORY );
  }

  @Override
  public void buildContent()
  {
    BorderLayoutData data;
    final VHMainController ctr = (VHMainController) getController();

    if ( isContentReady() )
      return;

    setLayoutOnChange( true );

    // Northern settings panel
    //
    ContentPanel panelNorth = new ContentPanel( new RowLayout( Style.Orientation.VERTICAL ) );
    panelNorth.setHeaderVisible( false );

    // season selector
    //
    d_seasonSelector = new VHSeasonSelector();
    ctr.loadSeasonsForClubHistory( d_seasonSelector, d_league.getId() );
    panelNorth.add( d_seasonSelector, new RowData( Style.DEFAULT, Style.DEFAULT, new Margins( 4, 4, 4, 4 ) ) );

    // standings type selector
    //
    d_standingsTypeSelector = new VHStandingsTypeSelector();
    panelNorth.add( d_standingsTypeSelector, new RowData( Style.DEFAULT, Style.DEFAULT, new Margins( 0, 4, 4, 4 ) ) );

    data = new BorderLayoutData( LayoutRegion.NORTH );
    data.setMargins( new Margins( 4, 4, 4, 4 ) );
    data.setSize( 58f );
    add( panelNorth, data );

    // event listeners
    //
    d_seasonSelector.getCombobox().addSelectionChangedListener( new SelectionChangedListener<VHJsData>() {
      @Override
      public void selectionChanged( SelectionChangedEvent<VHJsData> se )
      {
        VHJsData seasonSelected = se.getSelectedItem();
        onSeasonSelected( seasonSelected );
      }
    } );

    d_standingsTypeSelector.getCombobox().addSelectionChangedListener( new SelectionChangedListener<VHJsData>() {
      @SuppressWarnings( "synthetic-access" )
      @Override
      public void selectionChanged( SelectionChangedEvent<VHJsData> se )
      {
        if ( !d_seasonSelector.isInitialised() )
          return;

        VHJsData st = se.getSelectedItem();
        ctr.loadHistoricStandings( d_standingsGrid, d_league.getString( "regularSeasonElementId" ),
            d_seasonSelector.getCombobox().getValue().getInt( "id" ), VHJsStandingsType.valueOf( st.getId() ) );
      }
    } );

    super.buildContent();
  }

  /**
   * Sets the main panel area based on the selected season and data availability. If data
   * is available, the standings table is shown, otherwise the no results panel will be
   * displayed.
   *
   * This method should be called when the season selector has received its data and then
   * every time the user chooses a different season.
   */
  public void onSeasonSelected( VHJsData seasonSelected )
  {
    if ( d_layoutDataCenter == null )
    {
      d_layoutDataCenter = new BorderLayoutData( LayoutRegion.CENTER );
      d_layoutDataCenter.setMargins( new Margins( 0, 4, 4, 4 ) );
    }

    if ( seasonSelected == null || !seasonSelected.getBoolean( "dataAvailable" ) )
    {
      if ( d_panelNoResult == null )
      {
        HtmlContainer html = new HtmlContainer( "<div style='margin-top: 15px; text-align: center; width: 100%;'>"
            + VHMain.getConstants().noDataForSelectedSeason() + "</div>" );
        d_panelNoResult = new ContentPanel( new BorderLayout() );
        d_panelNoResult.add( html, new BorderLayoutData( LayoutRegion.CENTER ) );
        d_panelNoResult.setHeaderVisible( false );
      }

      d_standingsTypeSelector.setEnabled( false );

      if ( d_widgetCurrent != null )
        d_widgetCurrent.removeFromParent();
      add( d_panelNoResult, d_layoutDataCenter );
      d_widgetCurrent = d_panelNoResult;
    }
    else
    {
      final VHMainController ctr = (VHMainController) getController();

      if ( d_standingsGrid == null )
        d_standingsGrid = new VHStandingsGrid( getController(), VHStandingsGrid.DisplayType.HISTORY );


      ctr.loadHistoricStandings( d_standingsGrid, d_league.getString( "regularSeasonElementId" ),
          seasonSelected.getInt( "id" ),
          VHJsStandingsType.valueOf( d_standingsTypeSelector.getCombobox().getValue().getId() ) );

      d_standingsTypeSelector.setEnabled( true );

      if ( d_widgetCurrent != null )
        d_widgetCurrent.removeFromParent();
      add( d_standingsGrid, d_layoutDataCenter );
      d_widgetCurrent = d_standingsGrid;
    }
  }
}
